﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

namespace MyImage
{
    public partial class ParseImageCopy : System.Web.UI.Page
    {

        #region Instance Fields
        // constants used to create URLs to this page
        public const String PAGE_NAME = "ParseImageCopy.aspx";
        //the image primary key for use when getting image data from database
        public const String IMAGE_ID = "img_pk";
        // height of the thumbnail created from the original image
        public static int THUMBNAIL_SIZE;
        // true if using thumbnail THUMBNAIL_SIZE for height, else it is used for width
        public static bool USE_SIZE_FOR_HEIGHT;
        #endregion]


        protected void Page_Load(object sender, EventArgs e)
        {
            byte[] imageData = null;
            MemoryStream ms = null;
            System.Drawing.Image fullsizeImage = null;
            String imageID = null;
            if (!Page.IsPostBack)
            {
                imageID = Request.QueryString[IMAGE_ID];
                imageData = DataAcess.GetPictureByID(int.Parse(imageID), true);

                // create an image from the byte array
                ms = new MemoryStream(imageData);
                fullsizeImage = System.Drawing.Image.FromStream(ms);

                Response.ContentType = "image/Jpeg";
                GetThumbnailImage ir = new GetThumbnailImage();

                // Load your image and perform any resizing here
                ir.File = fullsizeImage;
                if (USE_SIZE_FOR_HEIGHT)
                    ir.Height = THUMBNAIL_SIZE;
                else
                    ir.Width = THUMBNAIL_SIZE;
                //get the thumbnail

                ir.GetThumbnail().Save(Response.OutputStream,
                 System.Drawing.Imaging.ImageFormat.Jpeg);

                //fullsizeImage.Save(Response.OutputStream,
                //    System.Drawing.Imaging.ImageFormat.Jpeg);

                //ms.Close();


            }
        }
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }
        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.Page_Load);
        }
    }
}
